
// This code creates 500 reshuffled treatment statuses for use in RBI.  

set seed 761741329
set sortseed 1386345216


*Here you will need to insert your own filepath and uncomment to navigate to the Replication directory.

*cd "...."

use "data/data_anonymized.dta", clear

foreach pair in treatedvscontrol {

	keep if fu_treatment_status!=""
	gen treated=fu_treatment_status!="control" 
	gen control=fu_treatment_status=="control" 

	local m = strpos("`pair'","vs")	
	local p1 = substr("`pair'",1,`m' -1)
	local p2 = substr("`pair'",`m'+2,.)
	local shortpair = substr("`p1'",1,2) + "vs" + substr("`p2'",1,2) 

	preserve
	
	keep village_code_correct lottery_union `p1' `p2'
	duplicates drop  

	sort lottery_union village_code_correct
	by lottery_union (village_code_correct), sort: gen n = _n

	save data/assignments_rand_`pair', replace

	keep lottery_union n `p1' `p2'
	save data/assignments_to_merge_`pair', replace

	use data/assignments_rand_`pair', clear

	keep lottery_union village_code_correct 
	forvalues i = 1(1)500 {
		quiet gen rand = runiform()
		by lottery_union, sort: egen n = rank(rand), field
		quiet merge 1:1 lottery_union n using data/assignments_to_merge_`pair'
		rename `p1' `p1'_`shortpair'_`i'
		rename `p2' `p2'_`shortpair'_`i'

		keep lottery_union village_code_correct `p1'* `p2'* 
	}

	save data/assignments_rand_`pair', replace

	restore

}

